<?PHP
/**
 * $Rev: 8 $
 * $Id: object.php 8 2013-03-02 20:35:16Z vad.oparin@gmail.com $
 */
abstract class object {
	protected $behaviors;

	public function __call($funcName, $arguments) {
		if (method_exists($this, $funcName)) {
			// @formatter:off
			return call_user_func_array(array($this, $funcName), $arguments);
        	// @formatter:on
		} else {
			foreach ($this->behaviors as $behavior) {
				if (method_exists($behavior, $funcName)) {
					// @formatter:off
					return call_user_func_array(array($behavior, $funcName), $arguments);
			        // @formatter:on
				}
			}
		}
	}

	public function addBehavior($behavior, $parameters = array()) {
		if (class_exists($behavior)) {
			$this->behaviors[$behavior] = new $behavior($parameters);
			$this->behaviors[$behavior]->init();
		}
	}

	public function removeBehavior($behavior) {
		if ($this->behaviors[$behavior]) {
			$this->behaviors[$behavior]->clear();
			unset($this->behaviors[$behavior]);
		}
	}

	public function clearBehaviors() {
		while ($behavior = array_shift($this->behaviors)) {
			$behavior->clear();
		}
	}

}
